// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'

import router from "./router/index.js"

//导入vue-resource
import VueResource from 'vue-resource' 
Vue.use(VueResource)
//设置请求的根路径
Vue.http.options.root = "http://www.liulongbin.top:3005"
//全局设置post时候表单数据格式组织形式
Vue.http.options.emulateJSON = true

//导入格式化时间的插件
import moment from 'moment'

//安装图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//全局的过滤器
Vue.filter('dateFormat',function(dateStr,patten = "YYYY-MM-DD HH:mm:ss"){
  return moment(dateStr).format(patten)
})

//注册vuex
import Vuex from 'vuex'
Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem("car") || "[]");

var store = new Vuex.Store({
  state:{
    car : car
  },
  mutations:{
    addToCar(state,goodsinfo){
      //点击加入购物车，把商品信息保存在store中的car中
      var flag = false;
      state.car.some(item =>{
        if(item.id == goodsinfo.id){
          item.count += parseInt(goodsinfo.count);
          flag = true;
          return true;
        }
      })
      if(!flag){
        state.car.push(goodsinfo);
      }
      localStorage.setItem("car",JSON.stringify(state.car));
    },
    updateCarCount(state,goodsinfo){
      state.car.some(item=>{
        if(item.id == goodsinfo.id){
          item.count = parseInt(goodsinfo.count);
          return true;
        }
      })
      //购物车中修改了商品数量，把最新的购物车数据保存到本地存储中
      localStorage.setItem("car",JSON.stringify(state.car));
    },
    removeFromCar(state,id){
      state.car.some((item,i) => {
        if(item.id == id){
          state.car.splice(i,1);
          return true;
        }
      })
      localStorage.setItem("car",JSON.stringify(state.car));
    },
    updateSelected(state,info){
      state.car.some(item=>{
        if(item.id == info.id){
          item.selected = info.seleted;
          return true
        }
      })
      localStorage.setItem("car",JSON.stringify(state.car))
    }
  },
  getters:{
    //类似于computed计算属性的值
    getAllCount(state){
      var c = 0;
      state.car.forEach(item =>{
         c += item.count
      })
      return c;
    },
    getGoodsInfoCount(state){
      var c = {};
      state.car.forEach(item=>{
        c[item.id] = item.count
      })
      return c;
    },
    getGoodsSelected(state){
      var s = {};
      state.car.forEach(item=>{
        s[item.id] = item.selected
      })
      return s;
    },
    getGoodsCountAndAmount(state){
      var o = {
        count : 0,
        amount : 0
      }
      state.car.forEach(item=>{
        if(item.selected){
          o.count += item.count;
          o.amount += item.price * item.count;
        }
      })
      return o;
      
    }
  }
})

//按需导入mint.ui的组件
// import { Header, Swipe, SwipeItem, Button, Lazyload } from 'mint-ui'
// Vue.component(Header.name, Header)
// Vue.component(Swipe.name, Swipe)
// Vue.component(SwipeItem.name, SwipeItem)
// Vue.component(Button.name, Button)
// Vue.use(Lazyload)
//为了懒加载有加载图标，先使用全局导入组件
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//导入mui的样式文件
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'



Vue.config.productionTip = false


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render:c=> c(App),
  store
})
